package demoxsgl_300.com.gouwu1500.zhi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demoxsgl_300.com.gouwu1500.R;
import demoxsgl_300.com.gouwu1500.adapter.JianKang_Adapter;
import demoxsgl_300.com.gouwu1500.bean.DangAn;
import demoxsgl_300.com.gouwu1500.http.API;
import demoxsgl_300.com.gouwu1500.http.Okhttp;
import demoxsgl_300.com.gouwu1500.utils.JSONUtils;

public class JianKangDangAnActivity extends Activity {

    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.lv_list)
    ListView lvList;
    JianKang_Adapter jianKang_adapter;

    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.imageView5)
    ImageView imageView5;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    @BindView(R.id.fanhui)
    TextView fanhui;
    ArrayList<DangAn> dangAns;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiankangdangan);
        ButterKnife.bind(this);
        tvLogin.setText("当前用户:" + Content.user_password);
        API.historyrecord(Content.user_password, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("1111", "xx" + st);
                Type type = new TypeToken<ArrayList<DangAn>>() {
                }.getType();
                dangAns = JSONUtils.parseJSONArray(st, type);
                jianKang_adapter.addItem(dangAns);
            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(JianKangDangAnActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
        jianKang_adapter = new JianKang_Adapter();
        lvList.setAdapter(jianKang_adapter);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(JianKangDangAnActivity.this, JieGuoFenXiActivity.class);
                intent.putExtra("time", dangAns.get(i).getTime());
                startActivity(intent);
            }
        });

    }

    @OnClick({R.id.fanhui, R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fanhui:
                finish();
                break;
            case R.id.tv_logout:
                Content.Login = false;
                finish();
                break;
        }
    }
}
